﻿
using AiBLSmartEdu.Module.ToDo.API.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace AiBLSmartEdu.Module.ToDo.API;

internal class ToDoBackgroundService : BackgroundService
{
    private readonly IServiceScopeFactory _serviceScopeFactory;

    public ToDoBackgroundService(IServiceScopeFactory serviceScopeFactory)
    {
        _serviceScopeFactory = serviceScopeFactory;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            using (var scope = _serviceScopeFactory.CreateScope())
            {
                var scopedService = scope.ServiceProvider.GetRequiredService<ToDoServices>();
                await scopedService.MarkExpiredToDoItemsAsync();
            }
            await Task.Delay(TimeSpan.FromMinutes(10), stoppingToken);
        }
    }
}